package com.zang.websocket;

import com.zang.model.entity.Employ;
import com.zang.websocket.listener.EmployListener;
import io.netty.handler.codec.http.HttpHeaders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yeauty.annotation.*;
import org.yeauty.pojo.Session;

/**
 * @author Zhang Qiang
 * @date 2019/10/22 9:26
 */
@Slf4j
@ServerEndpoint(prefix = "webSocket")
@Component
public class WebSocket {

    @Autowired
    EmployListener employListener;

    @OnOpen
    public void onOpten(Session session, HttpHeaders httpHeaders){
        log.info("打开新连接");
        log.info("session id :" + session.channel().id());
    }

    @OnClose
    public void onClose(Session session){
        log.info("关闭连接，Session:" + session.channel().id().asLongText());
    }

    @OnMessage
    public void onMessage(Session session, String message) throws Exception {
        Employ employ = employListener.listener( message, session.channel().id().asLongText());
        session.sendText("找到" + message + "的信息：" + employ.toString());
    }

    @OnError
    public void onError(Session session, Throwable throwable){
        log.info("session " + session.channel().id().asLongText() + " throwable:" );
        throwable.printStackTrace();
    }

    @OnEvent
    public void onEvent(){
        log.info("触发事件");
    }


}
